class ListNode{
    int val;
    ListNode prev;
    ListNode next;
    public ListNode(int val){
        this.val = val;
    }
}
public class MyDoubleList {
    ListNode head = new ListNode(-1);//傀儡节点
    ListNode last = this.head;

    //打印双向链表
    public void display() {
        ListNode cur = head;
        while (cur != null) {
            System.out.print(cur.val + " ");
            cur = cur.next;
        }
        System.out.println();
    }

    //得到双向列表的长度
    public int size() {
        ListNode cur = head;
        int count = 0;
        while (cur != null) {
            count++;
            cur = cur.next;
        }
        return count;
    }

    //查找关键字key是否包含在双向链表当中
    public boolean contains(int key) {
        ListNode cur = head;
        while (cur != null) {
            if (cur.val == key) {
                return true;
            }
            cur = cur.next;
        }
        return false;
    }

    //头插法
    public void addFirst(int data) {
        ListNode cur = new ListNode(data);
        if (this.head.next == null) {
            this.head.next = cur;
            cur.prev = this.head;
            this.last = cur;
        } else {
            cur.next = this.head.next;
            cur.prev = this.head;
            cur.next.prev = cur;
            this.head.next = cur;
            this.last = cur.next;
        }
    }

    //尾插法
    public void addLast(int data) {
        ListNode cur = new ListNode(data);
        if (this.head.next == null) {
            this.head.next = cur;
            this.last = cur;
            cur.prev = this.head;
        } else {
            this.last.next = cur;
            cur.prev = this.last;
            this.last = cur;
        }
    }

    //删除第一次出现的值为key的节点
    public void remove(int key) {
        ListNode cur = this.head;
        while (cur != null) {
            if (cur.val == key) {
                if (cur == this.head) {
                    this.head = this.head.next;
                    if (this.head != null) {
                        this.head.prev = null;
                    } else {
                        this.last = null;
                    }
                } else if (cur == this.last) {
                    this.last = this.last.prev;
                    this.last.next = null;
                } else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
                return;
            }
            cur = cur.next;
        }
        System.out.println("没有对应的key值");
    }

    //删除所有为key的节点
    public void allRemove(int key) {
        ListNode cur = this.head;
        int a = size();
        while (cur != null) {
            if (cur.val == key) {
                if (cur == this.head) {
                    this.head = this.head.next;
                    if (this.head != null) {
                        this.head.prev = null;
                    } else {
                        this.last = null;
                    }
                } else if (cur == this.last) {
                    this.last = this.last.prev;
                    this.last.next = null;
                } else {
                    cur.prev.next = cur.next;
                    cur.next.prev = cur.prev;
                }
            }
            cur = cur.next;
        }
        int b = size();
        if (a == b) {
            System.out.println("没有对应的key值");
        }
    }

    //在任意位置插入一个节点
    //先判断插入位置的合法性
    public int search(int index, int data) {
        if (index < 0 || index > size()) {
            System.out.println("坐标不合法，无法插入");
            return 1;
        } else if (index == 0) {
            addFirst(data);
            return 1;
        } else if (index == size()) {
            addLast(data);
            return 1;
        }
        return 2;
    }

    public void addIndex(int index, int data) {
        ListNode node = new ListNode(data);
        ListNode cur = this.head;
        if (search(index, data) != 1) {
            while (index != 0) {
                cur = cur.next;
                index--;
            }
            node.next = cur;
            node.prev = cur.prev;
            cur.prev.next = node;
            cur.prev = node;
        }
    }

    //清空链表数据
    public void clear() {
        ListNode cur = this.head;
        while (cur != null) {
            cur = cur.next;
            this.head.next = null;
            this.head.prev = null;
            this.head = cur;
        }
        this.last = null;
    }
}


























